package com.iterative.mysql.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iterative.common.persistence.Page;
import com.iterative.common.util.StringUtils;
import com.iterative.common.web.BaseController;
import com.iterative.mysql.util.MysqlDML;
@Controller
@RequestMapping("/mysql/query")
public class MysqlQueryController extends BaseController{
	private  static Logger logger = Logger.getLogger(MysqlQueryController.class);
	
	
	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "mysql/queryList";
	}
	
	@RequestMapping(value="json")
	@ResponseBody
	public Map<String, Object> getMysqlConnects1(HttpServletRequest request ,HttpServletResponse response){
		Page<Object> page = getPage(request);
		String sql = request.getParameter("sql");
		String tableName = request.getParameter("tableName");
		if(!StringUtils.isBlank(tableName)){
			
//			MySqlImportAndExport.export();
		}
		List list  = null;
		if(StringUtils.isBlank(sql)){
			return getEasyUIData(page);
		}
		if(sql.contains("select")){
			list = MysqlDML.getSelectResult(sql);
		}else{
//			excuteBySql(sql);
		}
		page.setResult(list);
		page.setTotalCount(list.size());
		return getEasyUIData(page);
		
	}
	
	
	
	
	
	
	
	
	
////	@RequestMapping(value="json")
////	@ResponseBody
//	public Map<String, Object> getMysqlConnects(HttpServletRequest request ,HttpServletResponse response)throws Exception{
//		List respList =null;
//		String sql = request.getParameter("sql");
//		if(StringUtils.isBlank(sql)){
//			return null;
//		}
//		Map<String, String>  tableMap = ApplicationContextUtil.getTables();
//		String tableName =null;
//		if(sql.contains("from")){
//			String sqlKey[] = sql.split("from");
//			sqlKey = sqlKey[1].trim().split(" ");
//			tableName = sqlKey[0];
//		}
//		if(sql.contains("select")){
//			List<Object> list = queryBySql(sql);
//			if(sql.split("from")[0].contains("*")){
//				for (int i=0;i<list.size();i++) {
//					Class<?> cls = Class.forName(tableMap.get(tableName));
//					 Field   fields[] = cls.getDeclaredFields();
//					 Field.setAccessible(fields,true);
//					 Method[] methods = cls.getDeclaredMethods();
//					 Constructor<?>[] constructors = cls.getConstructors();
//					 Object obj = constructors[0].newInstance();
//					 for(Field field : fields){
//						 for(int j=0; j<methods.length; j++){
//							 
//						 }
//					 }
//					 
//					 
//					 
//				}
//			}
//			
//		}else{
//			excuteBySql(sql);
//		}
////		page.setResult(list);
////		page.setTotalCount(list.size());
////		return getEasyUIData(page);
//		return null;
//	}
//	
	
	
	
	
}
